using System.Collections.Generic;
using System.Xml.Linq;
using TickSharp.Core.Infrastructure.Serializers;
using TickSharp.Core.Interfaces;
using TickSharp.Core.Interfaces.Repositories;
using TickSharp.Core.Model;

namespace TickSharp.Core.Infrastructure.Repositories
{
    public class TaskRepository : RepositoryBase, ITaskRepository
    {
        private readonly ISerializer<Task> _serializer;

        public TaskRepository(IApiConnectionInfo apiConnectionInfo, ISerializer<Task> serializer) : base(apiConnectionInfo)
        {
            _serializer = serializer;
        }

        public TaskRepository(IApiConnectionInfo apiConnectionInfo) : this(apiConnectionInfo, new TaskSerializer())
        {
        }

        /// <summary>
        /// Optional parameters:
        /// task_id
        /// open [true|false]
        /// task_billable [true|false]
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Task> List(int projectId)
        {
            string uri = BuildUrl("tasks", string.Format("project_id={0}", projectId));
            XElement elements = GetResponse(uri);
            return _serializer.Deserialize(elements);
        }
    }
}